package org.shiro.demo.entity;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "sys_role")
public class Role implements Serializable
{

	private static final long serialVersionUID = 6177417450707400228L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	private Long id;
	
	@Column(length = 255)
	private String name;
	
	@Column(length = 255)
	private String description;

	@ManyToMany(mappedBy = "roles")
	@Basic(fetch = FetchType.LAZY)
	private Set<User> users;

	@ManyToMany(cascade = {
				CascadeType.PERSIST, 
				CascadeType.REFRESH,CascadeType.MERGE 
			}, fetch = FetchType.LAZY)
	@JoinTable(name = "sys_role_permission", 
		joinColumns = { @JoinColumn(name = "role_id", updatable = false) }, 
		inverseJoinColumns = { @JoinColumn(name = "permission_id", updatable = false) }
	)
	private List<Permission> permissions;

	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public Set<User> getUsers()
	{
		return users;
	}

	public void setUsers(Set<User> users)
	{
		this.users = users;
	}

	public List<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

}
